package cn.itcast.ssh.service.impl;

import cn.itcast.ssh.service.ProcessDefinitionService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * @author xiongshiyan at 2021/3/16 , contact me with email yanshixiong@126.com or phone 15208384257
 */
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService{
    private RepositoryService repositoryService;

    private RuntimeService runtimeService;

    private TaskService taskService;

    private FormService formService;

    private HistoryService historyService;


    @Override
    public List<ProcessDefinition> findAllLatestVersions() {
        List<ProcessDefinition> list = repositoryService//
                .createProcessDefinitionQuery()//
                .orderByProcessDefinitionVersion().asc()//使用流程定义的版本升序排列
                .list();
        /**
         * Map<String,ProcessDefinition>
         map集合的key：流程定义的key
         map集合的value：流程定义的对象
         map集合的特点：当map集合key值相同的情况下，后一次的值将替换前一次的值
         */
        Map<String, ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
        if(list!=null && list.size()>0){
            for(ProcessDefinition pd:list){
                map.put(pd.getKey(), pd);
            }
        }
        return new ArrayList<>(map.values());
    }

    @Override
    public void deleteByKey(String processDefinitionKey) {
        //先使用流程定义的key查询流程定义，查询出所有的版本
        List<ProcessDefinition> list = repositoryService//
                .createProcessDefinitionQuery()//
                .processDefinitionKey(processDefinitionKey)//使用流程定义的key查询
                .list();
        //遍历，获取每个流程定义的部署ID
        if(list!=null && list.size()>0){
            for(ProcessDefinition pd:list){
                //获取部署ID
                String deploymentId = pd.getDeploymentId();
                repositoryService//
                        .deleteDeployment(deploymentId, true);
            }
        }
    }

    @Override
    public void deployByZip(String deployName, ZipInputStream zipInputStream) {
        Deployment deployment = repositoryService//与流程定义和部署对象相关的Service
                .createDeployment()//创建一个部署对象
                .name(deployName)//添加部署的名称
                .addZipInputStream(zipInputStream)//指定zip格式的文件完成部署
                .deploy();//完成部署
    }

    @Override
    public InputStream getProcessImageAsStream(String processDefinitionId) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();
        return repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getDiagramResourceName());
    }

    public void setHistoryService(HistoryService historyService) {
        this.historyService = historyService;
    }

    public void setFormService(FormService formService) {
        this.formService = formService;
    }

    public void setRuntimeService(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }
    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    public void setRepositoryService(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    public RepositoryService getRepositoryService() {
        return repositoryService;
    }

    public RuntimeService getRuntimeService() {
        return runtimeService;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public FormService getFormService() {
        return formService;
    }

    public HistoryService getHistoryService() {
        return historyService;
    }
}
